<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/9/17
 * Time: 9:41
 * YY: 294963672@qq.com
 */

namespace App\Http\Controllers\Admin\Goods;


use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Goods\GetListRequest;
use App\Http\Response\ApiCode;
use App\Models\Goods;
use Illuminate\Http\Request;
use MarcinOrlowski\ResponseBuilder\ResponseBuilder;
use Symfony\Component\HttpFoundation\Response;

class GoodsController extends Controller
{
    /**
     * @param Request $request
     * @return Response
     * 商品列表
     */
    public function getList(GetListRequest $request): Response
    {
        $validated = $request->validated();
        return ResponseBuilder::asSuccess(ApiCode::HTTP_OK)
            ->withHttpCode(ApiCode::HTTP_OK)
            ->withData(Goods::getList($validated))
            ->withMessage(__('message.common.search.success'))
            ->build();
    }

    /**
     * 创建
     *
     * @param Request $request
     * @return Response
     */
    public function create(Request $request): Response
    {
//        $validated = $request->validated();
        return ResponseBuilder::asSuccess(ApiCode::HTTP_OK)
            ->withHttpCode(ApiCode::HTTP_OK)
            ->withData(Goods::create($request->all()))
            ->withMessage(__('message.common.create.success'))
            ->build();
    }

    /**
     * 更新
     *
     * @param Request $request
     * @return Response
     */
    public function update(Request $request): Response
    {
        //  $validated = $request->validated();
        $resultData = Goods::updateSave($request->all());
        if ($resultData['result']) {
            return ResponseBuilder::asSuccess(ApiCode::HTTP_OK)
                ->withHttpCode(ApiCode::HTTP_OK)
                ->withData($resultData['admin'])
                ->withMessage(__('message.common.update.success'))
                ->build();
        }

        return ResponseBuilder::asError(ApiCode::HTTP_BAD_REQUEST)
            ->withHttpCode(ApiCode::HTTP_BAD_REQUEST)
            ->withMessage(__('message.common.update.fail'))
            ->build();
    }


    /**
     * 删除
     *
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function delete(Request $request): Response
    {
        $id = $request->post('id', 0);
        $goods = Goods::query()->find($id);
        if ($goods) {
            $goods->delete();
            return ResponseBuilder::asSuccess(ApiCode::HTTP_OK)
                ->withHttpCode(ApiCode::HTTP_OK)
                ->withMessage(__('message.common.delete.success'))
                ->build();
        }

        return ResponseBuilder::asError(ApiCode::HTTP_BAD_REQUEST)
            ->withHttpCode(ApiCode::HTTP_BAD_REQUEST)
            ->withMessage(__('message.common.delete.fail'))
            ->build();
    }


    /**
     * 商品详情
     *
     * @param Request $request
     * @return Response
     */
    public function details(Request $request): Response
    {
        $id = $request->post('id', 0);
        $goods = Goods::find($id);
        if ($goods) {
            return ResponseBuilder::asSuccess(ApiCode::HTTP_OK)
                ->withHttpCode(ApiCode::HTTP_OK)
                ->withData($goods)
                ->withMessage(__('message.common.search.success'))
                ->build();
        }

        return ResponseBuilder::asError(ApiCode::HTTP_BAD_REQUEST)
            ->withHttpCode(ApiCode::HTTP_BAD_REQUEST)
            ->withMessage(__('message.common.search.fail'))
            ->build();
    }
}